package com.enqualcomm.kids.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by yinzhiqun on 2016/3/9.
 */
public class HoldSpeakButton extends FreshView {

    public HoldSpeakButton(Context context) {
        this(context,null);
    }

    public HoldSpeakButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HoldSpeakButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    Subscription subscription;
    boolean canceled;
    boolean started;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                started = false;
                canceled = false;
                subscription= Observable.timer(200, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Action1<Long>() {
                            @Override
                            public void call(Long aLong) {
                                if (mListener!=null){
                                    mListener.onStart();
                                    started = true;
                                }
                            }
                        });
                break;
            case MotionEvent.ACTION_MOVE:
                if (canceled){
                    break;
                }
                //判断是否滑出界
                float x = event.getX();
                float y = event.getY();

                if (x>getMeasuredWidth() ||x<0 ||y>getMeasuredHeight()|| y<0){
                    if (subscription!=null&&!subscription.isUnsubscribed()){
                        subscription.unsubscribe();
                    }
                    if (started&&mListener!=null){
                        mListener.onCancel();
                    }
                    canceled = true;//不再处理后续事件
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (canceled){
                    break;
                }
                if (subscription!=null&&!subscription.isUnsubscribed()){
                    subscription.unsubscribe();
                }
                if (started&&mListener!=null){
                    mListener.onEnd();
                }
                break;
        }


        return super.onTouchEvent(event);
    }

    OnHoldSpeakListener mListener;
    public void setOnHoldSpeakListener(OnHoldSpeakListener listener){
        this.mListener = listener;
    }

    public interface OnHoldSpeakListener{

        /**
         * 触发长按
         */
        void onStart();

        /**
         * 松开手指
         */
        void onEnd();

        /**
         * 手指滑动到其他地方
         */
        void onCancel();
    }

}
